from wisdoms.processManagement.MyPriorityQueue import MyPriorityQueue
from wisdoms.Memory.MemoryController import MemoryController
from wisdoms.File import FileManage
from wisdoms import deviceManager
from wisdoms.processManagement.PCB import PCB

# 进程检查清单,其中存储进程的PID编号，防止PID重名
processCheckList = []

### 初始化设备
deviceManager.main()
# 文件初始化
FileManage.main()
# 内存控制器初始化
MemoryController = MemoryController()
# 文件控制器初始化
FileController = FileManage.PM(MemoryController)
# 文件显示实例
FileShow = FileManage.SC()
# 进程初始化

#

# 界面显示
# 内存界面接口
def memoryShow(name = 'ALL'):
    return MemoryController.show(name)
# 设备界面接口
def deviceShow():
    return deviceManager.displayDCT()
# 文件界面接口
def fileShow():
    global FileShow
    s=FileShow.pac()
    print(s)
    return FileShow.tojson(FileShow.exhibit())

# 检查创建进程的合法性，输入PCB表的各种参数，若不重名，则返回创建好的内存
# 注意，在第一次使用前清空初始化proceeCheckList 表
def processCheck(PID,CpuTime, AllResource,re_mSize = 10,intoTime=0,name='null',priority=0,):
    # 检查PID是否已经存在
    if PID >= 0:
        try:
            # 检查进程ID是否存在
            processCheckList.index(PID)
            # 已经存在时返回空值
            return None
        except:
            # 不存在,记录该PID,并返回PCB对象
            processCheckList.append(PID)
            return PCB(PID,CpuTime, AllResource, re_mSize, intoTime=0, name='null', priority=0)
    else:
        print('PID格式输入错误')
        return None

# 名称：requestSemaphore
# 作用：信号量申请
# 输入：semaphore(字典型,信号量)，request（True:申请信号量，False:释放信号量），mutex（列表，存semaphore的key，处理信号量）
# 输出：True（信号量处理成功），False(信号量处理失败)
# 处理：一次性申请信号量，如果无法申请，则释放当前已有的信号量，避免死锁
def requestSemaphore(request,semaphore,mutex):
    bool = True
    if request:
         # 申请信号量
        lists=[]    # 用于申请信号量失败时返还所有信号量
        for i in mutex:
            if semaphore[i] != 0:
                lists.append(i);
                semaphore[i]=semaphore[i]-1
            else:
                bool = False
                break
        if not bool:
            # 此时申请失败，释放所有拥有的信号量
            for i in lists:
                semaphore[i]=semaphore[i]+1
    else:
       # 释放信号量
        for i in mutex:
            semaphore[i] = semaphore[i]+1
    return bool

#判断列表中是否存在PID相同的PCB
def comparePID(PID,PCBList):
    for pcb in PCBList:
        if PID == pcb.PID:
            return True
    return False

# 比较两个队列元素大小是否相同
def compareList(Alist,Blist):
    num = 0
    for i in Alist:
       if i == Blist[num]:
           num = num + 1
       else:
           return False
    return True

# 移除列表中的元素
def removePcbElement(PCB,overRuning):
    for overPCB in overRuning:
        if PCB.PID == overPCB.PID:
            overRuning.remove(overPCB)

# 将进程写入内存
def write_process(PCB, size):
    global MemoryController
    return MemoryController.write_process(PCB, size)
# 将进程从内存中删除
def delete_process(PID,size):
    global MemoryController
    return MemoryController.delete_process(PID, size)

# 请求内存资源
def requestMemory(PCB,size,requests):
    if requests:
    # 申请内存
        if write_process(PCB,size):
            # 内存申请成功，修改PCB现有内存
            PCB.Now_mSize = PCB.Now_mSize + PCB.re_mSize
            return True
        else:
            return False
    else:
    #删除内存
        if PCB.MAllRelease:
            #此时释放掉所有的内存(先将现有的内存放入到申请大小中)
            PCB.re_mSize = PCB.Now_mSize
            PCB.Now_mSize = 0
            return delete_process(PCB.PID,PCB.re_mSize)
        else:
            PCB.Now_mSize = PCB.Now_mSize - PCB.re_mSize
            if PCB.Now_mSize < 0:
                PCB.Now_mSize = 0
            return delete_process(PCB.PID,PCB.re_mSize)

#提醒内存将PCB从外存中移入内存
def noticeMemory(PID):
    MemoryController.dispatch_in(PID)


#请求设备资源
def requestDevice(PCB,deviceName,state):
    return deviceManager.requestDevice(PCB, deviceName, state)

# 创建文件
def createFile (path,size):
    return FileController._mk(path, size)
# 删除文件
def deleteFile(path,type):
    FileController._del(path, type)
# 查询文件
def findFile(path):
    FileController._Search(path, p=1)

# 请求文件操作
def requestFile(PCB):
    if PCB.Frequest == 'storage':
        return createFile(PCB.FileName, PCB.re_fSize)
    elif PCB.Frequest == 'delete':
        deleteFile(PCB.FileName, PCB.Ftype)
        return None
    else:
        findFile(PCB.FileName)
        return None

# 请求资源
def requestResource(max, allocation, need):
    return deviceManager.Banker(max, allocation, need)

# 处理单个事件
def eventAlongRead(pcb,event):
    #内存事件处理
    if event['task_application'] == 'M':
        pcb.task_application = 'M'
        pcb.Mrequest = event['Mrequest']
        pcb.re_mSize = event['re_mSize']
        pcb.MAllRelease = event['MAllRelease']
    #设备事件处理
    elif event['task_application'] == 'D':
        pcb.task_application = 'D'
        pcb.re_Dname = event['re_Dname']
        pcb.DeviceTime = event['DeviceTime']
        pcb.Drequest = event['Drequest']
    #文件事件处理
    elif event['task_application'] == 'F':
        pcb.task_application = 'F'
        pcb.Frequest = event['Frequest']
        pcb.FileName = event['FileName']
        pcb.re_fSize = event['re_fSize']
        pcb.Ftype = event['Ftype']
    #资源事件处理
    elif event['task_application'] == 'R':
        pcb.task_application = 'R'
        pcb.ResourceTime = event['ResourceTime']
        pcb.re_Resource = event['re_Resource']
        pcb.Rrequest = event['Rrequest']
    #信号量事件处理(由于信号量总量在一开始便已经输入，故此处不传该数据)
    else:
        pcb.task_application = event['task_application']
        pcb.re_mutex = event['re_mutex']
        pcb.Srequest = event['Srequest']

# 处理多种事件
def eventRead(pcb):
    if len(pcb.event)!=0:
        event = pcb.event[0]
        #先判断是否cpuTime符合触发事件的日期
        if(event['CpuTime'] >= pcb.CpuTime):
            #适合，则将该事件中pcb中弹出
            pcb.event.pop(0);
            #并根据状态字处理该事件
            eventAlongRead(pcb, event)
        else:
            #默认将其重新放回CPU就绪队列
            pcb.task_application = 'C'
    else:
        #默认将其重新放回CPU就绪队列
        pcb.task_application = 'C'

# 事件处理(根据具体情况将其写入不同的队列之中)
def eventHandle(pcb,Cready,Rapply,Fapply,Mapply,Sem_apply,Dapply,proHandleMode):
    #读取状态，根据不同的状态进行处理
    if pcb.task_application == 'C':
        #将进程重新写入就绪队列中(不同的调度算法处理不同)
        if proHandleMode == 'FIFS':
            #先来先到
            Cready.push(pcb)
        elif proHandleMode == 'SJF':
            #短作业优先
            Cready.append(pcb)
        elif proHandleMode == 'usually':
            #通常模式，从别的阻塞队列进入就绪队列时
            Cready.append(pcb)
        else:
            #时间片轮转(暂时这么写)
            Cready.append(pcb)
    elif pcb.task_application == 'D':
        #处理设备事件(申请)
        Dapply.append(pcb)
    elif pcb.task_application == 'R':
        #处理资源事件(申请)
        Rapply.append(pcb)
    elif pcb.task_application == 'D':
        #处理文件事件(申请)
        Fapply.append(pcb)
    elif pcb.task_application == 'M':
        #处理内存事件(申请)
        Mapply.append(pcb)
    elif pcb.task_application == 'F':
        #处理文件事件(申请)
        Fapply.append(pcb)
    else:
        #处理信号量事件(申请)
        Sem_apply.append(pcb)

# 资源分配
def resourceAllocation(PCB,state):
    num = 0
    if state:
        for r in PCB.re_Resource:
            PCB.Now_Resource[num] = PCB.Now_Resource[num] + r
            num += 1
    else:
        for r in PCB.re_Resource:
            PCB.Now_Resource[num] = PCB.Now_Resource[num] - r
            num += 1

# 进程管理,优化后
def processManage(Mode,PCBList,slot_duration = 1):
    #slot_duration 时间片大小
    #计数器，用以计算cpu的运行时间
    runtime = 0
    #确定调度方式（先来先到 FIFS，短作业优先 SJF，时间片轮转 RR，优先级调度 P）
    if Mode == 'SJF':
        #以时间为优先级
        key = lambda x:x.Counter
        #升序
        reverse = False
    else:
        #以优先级数为优先级
        key = lambda x:x.priority
        #降序
        reverse = True

    #创建作业进程进入内存的序列
    #  在每秒里，CPU中断工作，让且仅让一个进程进入到内存中
    IntoList = MyPriorityQueue(key,reverse)

    #创建进程就绪队列
    Cready=MyPriorityQueue(key,reverse)
    #正在运行的进程
    runPCB = None
    #时间片计数器(计数器等于时间片大小时，切换时间片)
    timeCount = 0

    #设备申请队列(准备申请设备的进程暂时停留在这)
    Dapply = []
    #设备运行队列(由于设备有多个，可同时并行，故此处可直接用列表)
    Druning = []
    #设备阻塞队列(此处根据其优先级完成设备的释放)
    Dblock=MyPriorityQueue(key,reverse)

    #资源申请队列
    Rapply=[]
    #资源运行队列(由于资源可有多个，可同时并行，故此处可直接用列表)
    Rruning=[]
    #运行完成但资源未释放队列(由于进程要得到所有的资源才能释放已有的资源，
    # 故使用此队列保存未释放进程以便使用银行家算法)
    overRuning=[]
    #资源阻塞队列(此处根据其优先级完成设备的释放)
    Rblock= MyPriorityQueue(key,reverse)

    #内存申请队列
    Mapply =  []
    #内存阻塞队列
    Mblock = MyPriorityQueue(key,reverse)
    #文件申请队列
    Fapply = []
    #文件阻塞队列
    Fblock = MyPriorityQueue(key,reverse)
    #信号量申请队列
    Sem_apply = []
    #信号量阻塞队列
    Sem_block=MyPriorityQueue(key,reverse)

    #进程完成队列
    Complete =[]

    #进程开始运行
    while(True):
        #PCBList为未进入内存的PCB
        if len(PCBList)!=0:
            #记录进入进程中的元素，并将其从PCBList中移除
            pcbRemoveList = []
            for pcb in PCBList:
                #将符合进入内存的进程放入到IntoList中
                if pcb.intoTime <= runtime:
                    if Mode == 'FIFS':
                        #先来先到，优先级普遍为0
                        pcb.priority = 0
                    IntoList.append(pcb)
                    pcbRemoveList.append(pcb)
            #将已经进入内存的PCB从PCBList中移除
            for removedPCB in pcbRemoveList:
                PCBList.remove(removedPCB)

        #从into中弹出进程，并根据其状态申请资源，并写入各种队列之中
        if len(IntoList) != 0:
            pcb = IntoList.popStart()
            #修正进程进入系统的时间
            pcb.intoTime = runtime
            #对进入内存的CPU做处理，判断其要申请什么资源，放入相应的队列中(
            #   刚进入内存的PCB无需对事件进行处理)
            if pcb.task_application == 'C':
                #将PCB加入到CPU申请就绪队列中
                Cready.append(pcb)
            elif pcb.task_application == 'D':
                #将PCB加入到设备申请就绪队列中
                Dapply.append(pcb)
            elif pcb.task_application == 'M':
                #将PCB加入到内存申请就绪队列中
                Mapply.append(pcb)
            elif pcb.task_application== 'R':
                #将PCB加入到资源申请队列中
                Rapply.append(pcb)
            elif pcb.task_application== 'S':
                #将PCB加入到信号量申请队列中
                Sem_apply.append(pcb)
            else:
                # 将PCB加入到文件申请队列中
                Fapply.append(pcb)
        # 处理各个队列中的进程
        # 防止刚进入内存就被跳过
        # isMblock = True
        isMapply = True
        # CPU就绪队列
        # 1 CPU处理(进程结束标记)
        # 1.1 根据时间片的长度调度PCB
        if timeCount == 0:
            # 1.1.1 从就绪队列中弹出要调度进程
            pcb = None
            while len(Cready)!= 0:
                pcb = Cready.popStart()
                if pcb.CpuTime != 0:
                    break;
                else:
                    #检查是否已经将内存释放
                    if pcb.Now_mSize == 0:
                        pcb.completeTime = runtime
                        #将已将完成任务的pcb写入到完成队列之中
                        Complete.append(pcb)
                        #将当前pcb置为空
                        pcb = None
                    else:
                        # 处理事件，将内存彻底释放
                        pcb.Mrequest = False
                        pcb.MAllRelease = True
                        pcb.task_application = 'M'
                        eventHandle(pcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                        # 标记，跳过进程处理
                        # isMblock = False
                        pcb = None


            if pcb is None:
                #此时当pcb为空时，检查各阻塞队列，若都为空，则说明CPU已经完成了所有的任务，故结束当前循环...
                # 判断内存申请队列中是否存在内存，若存在，则先进行内存的处理
                if len(Mapply) !=0 or len(Druning)!=0 or len(Dapply) != 0 or len(Rruning)!=0 or len(Rapply) or len(Rblock)!= 0:
                    isMapply = False    #False表示存在，True表示不存在
                else:
                    break;
            else:
                #非空，将该进程放入正在运行序列
                runPCB = pcb
        #此处表示刚有进程进入内存
        if isMapply :
            # 1.2 处理正在运行的PCB(runPCB)
            # 1.2.1 通知内存将进程从对换区换到内存中，并修改内存最后使用时间
            noticeMemory(runPCB.PID)
            runPCB.MLastTime = runtime
            # 1.2.2 减少CpuTime
            runPCB.CpuTime -= 1
            # 当进程完成时的标记，当标记为False时，则不修改timeCount的值
            isTimeOver = True

            if runPCB.CpuTime == 0:
                # 1.2.3 当等于0时,读取事件,此时timeCount=0,为了将其放在队列最前，
                #   此时设置优先级为50最大,或者Counter = 0此时优先级最大
                if Mode == 'SJF':
                    runPCB.Counter = 0
                else:
                    runPCB.priority = 50
                eventRead(runPCB)
                # 1.2.4 根据事件将进程挂在各种队列中
                eventHandle(runPCB, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, Mode)
                # 1.2.5 将标记置为False，通过此避免重复的事件处理
                isTimeOver = False
                timeCount = 0
            # 1.2.6 根据标记处理相关事件
            if isTimeOver :
                # 1.2.6.1 时间片计数器+1，并判断是否时间片已经到达
                timeCount = timeCount+1
                if timeCount == slot_duration:
                    #1.2.6.2 当时间片到时,进行相应处理,timeCount = 0
                    timeCount = 0
                    #1.2.6.3 读取事件，处理事件
                    eventRead(runPCB)
                    eventHandle(runPCB, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, Mode)

        # 2 设备处理
        # 2.1 将设备申请队列中的进程全进行设备的分配
        #    (分配成功,则进入运行队列。否则则进入阻塞队列)
        for pcb in Dapply:
            if requestDevice(pcb, pcb.re_Dname, pcb.Drequest):
                Druning.append(pcb)
            else:
                Dblock.append(pcb)
        # 2.2 将申请队列清空
        Dapply.clear()
        # 2.3 处理设备运行队列中的pcb进程
        temporary =[] #用作暂时存储
        temRun = [] # 暂时存储添加到运行队列中的PCB
        for pcb in Druning:
            if pcb.DeviceTime !=0:
                #2.3.1 减少PCB设备运行时间
                pcb.DeviceTime = pcb.DeviceTime - 1
            else:
                # 2.3.2 此时设备使用完成，释放设备
                temporary.append(pcb)
                pcb.Drequest = False
                requestDevice(pcb, pcb.re_Dname, pcb.Drequest)
                # 2.3.3 读取事件
                overDeviceName = pcb.re_Dname #先将要释放的设备名记住
                eventRead(pcb)
                # 2.3.4 处理事件
                eventHandle(pcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                # 2.3.5 扫描阻塞队列
                temPCB = 0
                for Dpcb in Dblock:
                    if Dpcb.re_Dname == overDeviceName:
                        # 当申请到设备资源时，则进入暂时运行队列保存
                        temRun.append(Dpcb)
                        temPCB = Dpcb
                        break;
                    # 2.3.6 将符合条件的pcb从 Dblock 中移除
                if temPCB != 0:
                    Dblock.remove(temPCB)
                # 2.4 将已经完成任务的进程从设备运行状态中移除
                for removePCB in temporary:
                    Druning.remove(removePCB)
        # 2.5从暂时运行队列中取出设备，并放入到运行队列中
        for tpcb in temRun:
            Druning.append(tpcb)

        # 3 资源处理
        # 3.1 将设备申请队列中的进程全进行设备的分配（将max,Allocation,Need参数写好）
        #    (分配成功,则进入运行队列。否则则进入阻塞队列)
        Max = []
        Allocation = []
        Rpcb = None
        for pcb in Rapply:
            #实际上由于此处Rapply通常仅仅只有一个，故在此处直接写入就好
            Rpcb = pcb
            Max.append(pcb.AllResource.copy())
            Allocation.append(pcb.Now_Resource.copy())

        for Rpcb in Rapply:
            #如果在overRuning已经存在，则将其从队列中去除
            removePcbElement(Rpcb, overRuning)

        # 将Rruning 队列中的资源写入到Max的表格中
        for pcb in Rruning:
            Max.append(pcb.AllResource.copy())
            Allocation.append(pcb.Now_Resource.copy())

        #将overRuning队列中的文件写入到Max的表格之中
        for overPCB in overRuning:
            #将阻塞的资源也写入到资源分配表中
            Max.append(overPCB.AllResource.copy())
            Allocation.append(overPCB.Now_Resource.copy())
           #将阻塞的资源也写入到资源分配表中
        for pcb in Rblock:
            Max.append(pcb.AllResource.copy())
            Allocation.append(pcb.Now_Resource.copy())
            #在阻塞队列中的进程无需申请任何资源



        # 3.2 采用银行家算法分配资源
        #    (分配成功,则进入运行队列。否则则进入阻塞队列)
        for Rpcb in Rapply:
            if requestResource(Max, Allocation, Rpcb.re_Resource.copy()):
                Rruning.append(Rpcb)
                resourceAllocation(Rpcb, Rpcb.Rrequest)
            else:
                Rblock.append(Rpcb)

        # 3.2.1 清空申请队列
        Rapply.clear()

        # 3.3 处理资源运行队列中的pcb进程
        temporary = [] # 用作暂时存储
        temRun = [] # 用作暂时存储要运行的队列
        for pcb in Rruning:
            if pcb.ResourceTime != 0:
                # 3.3.1 减少PCB资源运行时间
                pcb.ResourceTime = pcb.ResourceTime - 1
            else:
                # 3.3.2 此时资源使用完成，释放进程
                temporary.append(pcb) # 存储从运行队列中移除已经完成的队列
                # 3.3.3 由于银行家算法仅仅只是检测其是否能分配，故需在此处释放(释放时无需使用银行家算法)
                isReOver = False # 阻塞队列根据此判断是否对已阻塞资源进行遍历
                # 3.3.3.1 当将所有的资源都获得时，才能释放其资源
                if compareList(pcb.AllResource, pcb.Now_Resource):
                    pcb.Now_Resource = [0, 0, 0]
                    pcb.re_Resource = [0, 0, 0]
                    # 将其从资源完成队列中删除
                    removePcbElement(pcb, overRuning)
                    isReOver = True
                else:
                    # 当没能拥有所用资源时，判断是否在overRuning队列中
                    try:
                        overRuning.index(pcb)
                    except ValueError:
                        # 不在则添加到里面
                        overRuning.append(pcb)
                # 3.3.4 读取事件
                eventRead(pcb)
                # 3.3.5 处理事件
                eventHandle(pcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                # 3.3.6 如果有释放的资源，则扫描阻塞队列,取出资源判断有没有合适的资源被利用
                if isReOver:
                    # 3.3.6.1 将Max Allocation Need 清空，并将阻塞队列中的相应填入其中
                    Max.clear();Allocation.clear()
                    # 3.3.6.2 将资源阻塞表，运行表，未释放资源表全部添加到Max Allocation Need中
                    # 阻塞表
                    for Bpcb in Rblock:
                        Max.append(Bpcb.AllResource.copy())
                        Allocation.append(Bpcb.Now_Resource.copy())
                    # 运行表
                    for RunPcb in Rruning:
                        if not comparePID(RunPcb.PID, temporary):
                            Max.append(RunPcb.AllResource.copy())
                            Allocation.append(RunPcb.Now_Resource.copy())
                    # 未释放表
                    for Opcb in overRuning:
                        Max.append(Opcb.AllResource.copy())
                        Allocation.append(Opcb.Now_Resource.copy())

                    # 3.3.6.2 将所需资源一个一个写入，并判断是否可以申请,可申请，则放入运行队列中
                    for Bpcb in Rblock:
                        if requestResource(Max, Allocation, Bpcb.re_Resource.copy()):
                            #分配成功,则进入运行队列，并结束
                            temRun.append(Bpcb)
                            resourceAllocation(Bpcb, Bpcb.Rrequest)
                            Rblock.remove(Bpcb)
                            break

        # 3.3 清除运行队列中的完结进程
        for removePCB in temporary:
            Rruning.remove(removePCB)
        # 3.4从temRun中读取要进入队列的进程
        for tpcb in temRun:
            Rruning.append(tpcb)

        # 4 内存处理
        # 4.1 将内存就绪队列中的进程取出
        #    (分配成功,则进入运行队列。否则则进入阻塞队列)
        for pcb in Mapply:
            # 4.2 处理内存
            #此处先检查,申请则将其修改，并调用事件处理函数。若释放，则在调用事件处理函数的同时，
            #扫描阻塞队列，看看是否有新的进程能申请到内存
            if pcb.Mrequest:
                # 4.2.1内存申请
                if requestMemory(pcb,pcb.re_mSize,pcb.Mrequest):
                    # 4.2.1.1 申请成功,并修改访问内存的时间
                    pcb.MLastTime = runtime
                    # 4.2.1.2 处理事件
                    eventRead(pcb)
                    eventHandle(pcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                else:
                    #申请失败,将其申请写入到阻塞队列中
                    Mblock.append(pcb)
            else:
                # 4.2.2内存释放
                if requestMemory(pcb,pcb.re_mSize,pcb.Mrequest):
                    #4.2.2.0事件处理
                    eventRead(pcb)
                    eventHandle(pcb,Cready,Rapply,Fapply,Mapply,Sem_apply,Dapply,'usually')
                    # 4.2.2.1 释放一般都成功,处理阻塞队列,找出合适的能够释放的内存
                    temporary=[] #存储将要从阻塞队列移除的进程
                    for Bpcb in Mblock:
                        #如果申请成功，退出当前
                        if requestMemory(Bpcb,Bpcb.re_mSize,Bpcb.Mrequest):
                            # 4.2.2.2 修改时间
                            Bpcb.MLastTime = runtime
                            # 4.2.2.3 处理事件
                            eventRead(Bpcb)
                            eventHandle(Bpcb,Cready,Rapply,Fapply,Mapply,Sem_apply,Dapply,'usually')
                            # 4.2.2.4 添加入移除队列中
                            temporary.append(Bpcb)
                        # 4.2.2.5 从阻塞队列中删除已经申请到内存的文件
                    for removePCB in temporary:
                        Mblock.remove(removePCB)
           # 4.3 清空内存队列
        Mapply.clear()

        # 5 文件处理
        # 5.1 从申请队列中读出文件
        for Fpcb in Fapply:
            # 5.1.1处理文件(此处为简化代码，默认文件的操作基本都能完成)
            if Fpcb.Frequest == 'storage':
                # 5.1.2 申请文件存储
                if requestFile(Fpcb):
                    # 5.1.2.1 申请成功，处理事件
                    eventRead(Fpcb)
                    eventHandle(Fpcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                else:
                    # 5.1.2.2 申请失败，进入阻塞
                    Fblock.append(Fpcb)
            elif Fpcb.Frequest == 'delete':
                # 5.1.3 删除文件
                requestFile(Fpcb)
                # 5.1.4 处理事件
                eventRead(Fpcb)
                eventHandle(Fpcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                # 5.1.5从阻塞队列中选择可创建的文件的进程进行创建
                temporary = []
                for Bpcb in Fblock:
                    if requestFile(Bpcb):
                        # 5.1.5.1 申请成功，处理事件
                        eventRead(Bpcb)
                        eventHandle(Bpcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                        # 5.1.5.2 将该序列添加至移除队列中
                        temporary.append(Bpcb)
                #5.1.3.4 从阻塞队列中移除
                for removePCB in temporary:
                    Fblock.remove(removePCB)
            else:
                # 5.1.3 查询文件
                requestFile(Fpcb)
        # 5.2 清空申请队列(由于文件一般不会申请失败，此处不对申请失败的进程做处理)
        Fapply.clear()

        # 6 信号量处理
        # 6.1 从信号量申请队列中读取信号量
        for Spcb in Sem_apply:
            if Spcb.Srequest:
                # 6.2信号量申请
                if requestSemaphore(Spcb.Srequest, Spcb.Semaphore, Spcb.re_mutex):
                    # 6.2.1 申请成功，处理事件
                    eventRead(Spcb)
                    eventHandle(Spcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                else:
                    # 6.2.2 申请失败，进入阻塞
                    Sem_block.append(Spcb)
            else:
                # 6.3 信号量释放
                requestSemaphore(Spcb.Srequest, Spcb.Semaphore, Spcb.re_mutex)
                # 6.4 事件处理
                eventRead(Spcb)
                eventHandle(Spcb, Cready, Rapply, Fapply, Mapply, Sem_apply, Dapply, 'usually')
                # 6.3.1 遍历信号量阻塞队列，寻找可获信号量的进程
                temporary = []
                for Bpcb in Sem_block:
                    if requestSemaphore(Bpcb.Srequest,Bpcb.Semaphore,Bpcb.re_mutex):
                        # 6.3.2 申请成功，处理事件
                        eventRead(Bpcb)
                        eventHandle(Bpcb,Cready,Rapply,Fapply,Mapply,Sem_apply,Dapply,'usually')
                        # 6.3.3 将该事件添加至移除队列
                        temporary.append(Bpcb)
                #6.4 从信号量阻塞队列中移除已经得到资源的信号量
                for removePCB in temporary:
                    Sem_block.remove(removePCB)
        # 6.5 清空信号量申请队列
        Sem_apply.clear()

        # 界面返回处理
        showProcess = []

        if runPCB is not None:
            runPCBdict = {"index": runPCB.PID, 'typeItem':0 ,'baseTime':runtime ,'duration':1}
            showProcess.append(runPCBdict)

        for DdictPCB in Druning:
            runPCBdict = {"index": DdictPCB.PID, 'typeItem':1, 'baseTime': runtime, 'duration': 1}
            showProcess.append(runPCBdict)

        for RdictPCB in Rruning:
            runPCBdict = {"index": RdictPCB.PID, 'typeItem': 2, 'baseTime': runtime, 'duration': 1}
            showProcess.append(runPCBdict)

        yield showProcess

        # 时间段+1
        runtime = runtime + 1
    #return Complete

# 先来先到算法演示
def showFIFS():
    pcb1 = PCB(1, 11, [5, 5, 5], 10)
    pcb2 = PCB(2, 11, [5, 5, 5], 10)
    pcb3 = PCB(3, 11, [5, 5, 5], 10)
    pcb4 = PCB(4, 11, [5, 5, 5], 10)
    pcb5 = PCB(5, 11, [5, 5, 5], 10)
    PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    iter = processManage('FIFS', PCBList)
    result = []
    while True:
        try:
            for dict in next(iter):
                result.append(dict)
            yield result
        except:
            break

# 短作业优先算法演示
def showSJF():
    pcb1 = PCB(1, 35, [5, 5, 5], 10)
    pcb2 = PCB(2, 34, [5, 5, 5], 10)
    pcb3 = PCB(3, 33, [5, 5, 5], 10)
    pcb4 = PCB(4, 32, [5, 5, 5], 10)
    pcb5 = PCB(5, 31, [5, 5, 5], 10)
    PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    iter = processManage('SJF', PCBList)
    result = []
    try:
        for dict in next(iter):
            result.append(dict)
        yield result
    except:
        yield result #暂时先这样

# 时间片轮转算法演示
def showRR():
    pcb1 = PCB(1, 35, [5, 5, 5], 10)
    pcb2 = PCB(2, 34, [5, 5, 5], 10)
    pcb3 = PCB(3, 33, [5, 5, 5], 10)
    pcb4 = PCB(4, 32, [5, 5, 5], 10)
    pcb5 = PCB(5, 31, [5, 5, 5], 10)
    PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    iter = processManage('RR', PCBList, 1)
    result = []
    try:
        for dict in next(iter):
            result.append(dict)
        yield result
    except:
        yield result #暂时先这样

# 设备调度展示
def showDevice():
    #  创建设备事件
    eventDeviceA1 = {}
    eventDeviceA1['CpuTime'] = 30
    eventDeviceA1['task_application'] = 'D'
    eventDeviceA1['re_Dname'] = '设备A1'
    eventDeviceA1['DeviceTime'] = 5
    eventDeviceA1['Drequest'] = True

    eventDeviceB1 = {}
    eventDeviceB1['CpuTime'] = 30
    eventDeviceB1['task_application'] = 'D'
    eventDeviceB1['re_Dname'] = '设备B1'
    eventDeviceB1['DeviceTime'] = 5
    eventDeviceB1['Drequest'] = True

    eventDeviceB2 = {}
    eventDeviceB2['CpuTime'] = 30
    eventDeviceB2['task_application'] = 'D'
    eventDeviceB2['re_Dname'] = '设备B2'
    eventDeviceB2['DeviceTime'] = 5
    eventDeviceB2['Drequest'] = True

    eventDeviceC1 = {}
    eventDeviceC1['CpuTime'] = 30
    eventDeviceC1['task_application'] = 'D'
    eventDeviceC1['re_Dname'] = '设备C1'
    eventDeviceC1['DeviceTime'] = 5
    eventDeviceC1['Drequest'] = True

    eventDeviceC2 = {}
    eventDeviceC2['CpuTime'] = 30
    eventDeviceC2['task_application'] = 'D'
    eventDeviceC2['re_Dname'] = '设备C2'
    eventDeviceC2['DeviceTime'] = 5
    eventDeviceC2['Drequest'] = True


    eventDeviceC3 = {}
    eventDeviceC3['CpuTime'] = 30
    eventDeviceC3['task_application'] = 'D'
    eventDeviceC3['re_Dname'] = '设备C3'
    eventDeviceC3['DeviceTime'] = 5
    eventDeviceC3['Drequest'] = True

    # 设备事件添加测试
    pcb1 = PCB(1, 35, [5, 5, 5], 10)
    pcb2 = PCB(2, 34, [5, 5, 5], 10)
    pcb3 = PCB(3, 33, [5, 5, 5], 10)
    pcb4 = PCB(4, 32, [5, 5, 5], 10)
    pcb5 = PCB(5, 31, [5, 5, 5], 10)
    #   添加同设备事件
    pcb1.event.append(eventDeviceA1)
    pcb2.event.append(eventDeviceA1)
    # #   一个进程中添加多个设备申请事件
    pcb3.event.append(eventDeviceB1)
    pcb3.event.append(eventDeviceB2)
    # #   在一个进程中申请多种设备
    pcb4.event.append(eventDeviceB1)
    pcb4.event.append(eventDeviceC1)
    #     在一个进程中添加三种设备
    pcb5.event.append(eventDeviceA1)
    pcb5.event.append(eventDeviceB1)
    pcb5.event.append(eventDeviceC1)

    # 事件处理
    PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    iter = processManage('RR', PCBList, 1)
    result = []
    try:
        for dict in next(iter):
            result.append(dict)
        yield result
    except:
        yield result  # 暂时先这样

# 资源与文件调度展示
def showReAndFile():
    # 资源事件
    eventResourceR1 ={}
    eventResourceR1['CpuTime'] = 30
    eventResourceR1['task_application'] = 'R'
    eventResourceR1['ResourceTime'] = 1
    eventResourceR1['re_Resource'] = [3, 3, 3]
    eventResourceR1['Rrequest'] = True

    eventResourceR2 ={}
    eventResourceR2['CpuTime'] = 29
    eventResourceR2['task_application'] = 'R'
    eventResourceR2['ResourceTime'] = 1
    eventResourceR2['re_Resource'] = [2, 2, 2]
    eventResourceR2['Rrequest'] = True

    eventResourceR3 ={}
    eventResourceR3['CpuTime'] = 28
    eventResourceR3['task_application'] = 'R'
    eventResourceR3['ResourceTime'] = 1
    eventResourceR3['re_Resource'] = [1, 2, 3]
    eventResourceR3['Rrequest'] = True

    eventResourceR4 ={}
    eventResourceR4['CpuTime'] = 27
    eventResourceR4['task_application'] = 'R'
    eventResourceR4['ResourceTime'] = 1
    eventResourceR4['re_Resource'] = [4, 3, 2]
    eventResourceR4['Rrequest'] = True

    # 添加文件事件
    eventFileF1={}
    eventFileF1['CpuTime'] = 30
    eventFileF1['task_application'] = 'F'
    eventFileF1['Frequest'] = 'storage'
    eventFileF1['FileName'] = 'C\\First\\newText\\newText1.txt'
    eventFileF1['re_fSize'] = 10
    eventFileF1['Ftype'] = 0

    eventFileF2 = {}
    eventFileF2['CpuTime'] = 30
    eventFileF2['task_application'] = 'F'
    eventFileF2['Frequest'] = 'storage'
    eventFileF2['FileName'] = 'C\\First\\newText\\newText2.txt'
    eventFileF2['re_fSize'] = 10
    eventFileF2['Ftype'] = 0

    eventFileF3 = {}
    eventFileF3['CpuTime'] = 28
    eventFileF3['task_application'] = 'F'
    eventFileF3['Frequest'] = 'storage'
    eventFileF3['FileName'] = 'C\\First\\newText\\newText3.txt'
    eventFileF3['re_fSize'] = 10
    eventFileF3['Ftype'] = 1

    eventFileF4 = {}
    eventFileF4['CpuTime'] = 28
    eventFileF4['task_application'] = 'F'
    eventFileF4['Frequest'] = 'storage'
    eventFileF4['FileName'] = 'C\\First\\newText\\newText4.txt'
    eventFileF4['re_fSize'] = 10
    eventFileF4['Ftype'] = 0

    eventFileF5 = {}
    eventFileF5['CpuTime'] = 26
    eventFileF5['task_application'] = 'F'
    eventFileF5['Frequest'] = 'storage'
    eventFileF5['FileName'] = 'C\\First\\oldText\\newText1.txt'
    eventFileF5['re_fSize'] = 10
    eventFileF5['Ftype'] = 0

    eventFileF6 = {}
    eventFileF6['CpuTime'] = 26
    eventFileF6['task_application'] = 'F'
    eventFileF6['Frequest'] = 'storage'
    eventFileF6['FileName'] = 'C\\First\\oldText\\newText2.txt'
    eventFileF6['re_fSize'] = 10
    eventFileF6['Ftype'] = 0

    eventFileF7 = {}
    eventFileF7['CpuTime'] = 25
    eventFileF7['task_application'] = 'F'
    eventFileF7['Frequest'] = 'delete'
    eventFileF7['FileName'] = 'C\\First\\newText\\newText4.txt'
    eventFileF7['re_fSize'] = 10
    eventFileF7['Ftype'] = 1

    eventFileF8 = {}
    eventFileF8['CpuTime'] = 25
    eventFileF8['task_application'] = 'F'
    eventFileF8['Frequest'] = 'delete'
    eventFileF8['FileName'] = 'C\\First\\newText\\newTex3.txt'
    eventFileF8['re_fSize'] = 10
    eventFileF8['Ftype'] = 1

    pcb1 = PCB(1, 35, [5, 5, 5], 10)
    pcb2 = PCB(2, 34, [5, 5, 5], 10)
    pcb3 = PCB(3, 33, [5, 5, 5], 10)
    pcb4 = PCB(4, 32, [5, 5, 5], 10)
    pcb5 = PCB(5, 31, [5, 5, 5], 10)

    # 资源事件添加
    pcb1.event.append(eventResourceR1)
    pcb1.event.append(eventResourceR2)
    pcb2.event.append(eventResourceR3)
    pcb2.event.append(eventResourceR4)
    # 文件事件添加
    pcb3.event.append(eventFileF1)
    pcb3.event.append(eventFileF2)
    pcb3.event.append(eventFileF3)
    pcb3.event.append(eventFileF4)
    pcb4.event.append(eventFileF5)
    pcb4.event.append(eventFileF6)
    pcb5.event.append(eventFileF7)
    pcb5.event.append(eventFileF8)
    PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    iter = processManage('RR', PCBList, 1)
    result = []
    try:
        for dict in next(iter):
            result.append(dict)
        yield result
    except:
        yield result  # 暂时先这样

# 信号量同步设置
def showSem():
    # 设置信号量
    semaphore = {'mex1': 1, 'mex2': 0, 'mex3': 0}
    # 信号量同步事件
    pcb1 = PCB(1, 5, [5, 5, 5], 10)
    pcb2 = PCB(2, 5, [5, 5, 5], 10)
    pcb3 = PCB(3, 5, [5, 5, 5], 10)

    pcb1.Semaphore = semaphore
    pcb2.Semaphore = semaphore
    pcb3.Semaphore = semaphore
    eventSem1={}
    eventSem1['CpuTime'] = 5
    eventSem1['task_application'] = 'S'
    eventSem1['re_mutex'] = ['mex1'] #获取 1
    eventSem1['Srequest'] = True

    eventSemR1 = {}
    eventSemR1['CpuTime'] = 2
    eventSemR1['task_application'] = 'S'
    eventSemR1['re_mutex'] = ['mex2','mex3'] #释放 2 3
    eventSemR1['Srequest'] = False

    eventSem2 = {}
    eventSem2['CpuTime'] = 4
    eventSem2['task_application'] = 'S'
    eventSem2['re_mutex'] = ['mex2']  # 获取 2
    eventSem2['Srequest'] = True

    eventSem3 = {}
    eventSem3['CpuTime'] = 10
    eventSem3['task_application'] = 'S'
    eventSem3['re_mutex'] = ['mex3']  # 获取 3
    eventSem3['Srequest'] = True

    pcb1.event.append(eventSem1)
    pcb1.event.append(eventSemR1)
    pcb2.event.append(eventSem2)
    pcb3.event.append(eventSem3)

    PCBList = [pcb1, pcb2, pcb3]
    iter = processManage('FIFS', PCBList)
    result = []
    try:
        for dict in next(iter):
            result.append(dict)
        yield result
    except:
        yield result  # 暂时先这样

# 联合展示
# 联合调度展示
def showUnite():
    pcb1 = PCB(1, 10, [5, 5, 5], 10)
    pcb2 = PCB(2, 10, [5, 5, 5], 10)
    pcb3 = PCB(3, 10, [5, 5, 5], 10)
    pcb4 = PCB(4, 10, [5, 5, 5], 10)
    pcb5 = PCB(5, 10, [5, 5, 5], 10)

    # 进程名设置
    pcb1.name = '进程1'
    pcb2.name = '进程2'
    pcb3.name = '进程3'
    pcb4.name = '进程4'
    pcb5.name = '进程5'

    # 设置申请内存大小
    pcb1.re_mSize = 30
    pcb2.re_mSize = 20

    # 设备事件创建
    eventDeviceA1 = {}
    eventDeviceA1['CpuTime'] = 8
    eventDeviceA1['task_application'] = 'D'
    eventDeviceA1['re_Dname'] = '设备A1'
    eventDeviceA1['DeviceTime'] = 5
    eventDeviceA1['Drequest'] = True

    eventDeviceB1 = {}
    eventDeviceB1['CpuTime'] = 7
    eventDeviceB1['task_application'] = 'D'
    eventDeviceB1['re_Dname'] = '设备B1'
    eventDeviceB1['DeviceTime'] = 5
    eventDeviceB1['Drequest'] = True

    eventDeviceB11 = {}
    eventDeviceB11['CpuTime'] = 8
    eventDeviceB11['task_application'] = 'D'
    eventDeviceB11['re_Dname'] = '设备B1'
    eventDeviceB11['DeviceTime'] = 5
    eventDeviceB11['Drequest'] = True

    eventDeviceC1 = {}
    eventDeviceC1['CpuTime'] = 6
    eventDeviceC1['task_application'] = 'D'
    eventDeviceC1['re_Dname'] = '设备C1'
    eventDeviceC1['DeviceTime'] = 3
    eventDeviceC1['Drequest'] = True

    eventDeviceC12 = {}
    eventDeviceC12['CpuTime'] = 3
    eventDeviceC12['task_application'] = 'D'
    eventDeviceC12['re_Dname'] = '设备C1'
    eventDeviceC12['DeviceTime'] = 3
    eventDeviceC12['Drequest'] = True

    # 设备事件添加
    pcb1.event.append(eventDeviceA1)
    pcb1.event.append(eventDeviceB1)
    pcb1.event.append(eventDeviceC1)

    pcb2.event.append(eventDeviceB11)
    pcb2.event.append(eventDeviceC12)

    # 资源事件创建
    eventResourceR1 ={}
    eventResourceR1['CpuTime'] = 6
    eventResourceR1['task_application'] = 'R'
    eventResourceR1['ResourceTime'] = 2
    eventResourceR1['re_Resource'] = [3, 3, 3]
    eventResourceR1['Rrequest'] = True

    eventResourceR2 ={}
    eventResourceR2['CpuTime'] = 4
    eventResourceR2['task_application'] = 'R'
    eventResourceR2['ResourceTime'] = 1
    eventResourceR2['re_Resource'] = [2, 2, 2]
    eventResourceR2['Rrequest'] = True

    eventResourceR3 ={}
    eventResourceR3['CpuTime'] = 8
    eventResourceR3['task_application'] = 'R'
    eventResourceR3['ResourceTime'] = 2
    eventResourceR3['re_Resource'] = [1, 2, 3]
    eventResourceR3['Rrequest'] = True

    eventResourceR4 ={}
    eventResourceR4['CpuTime'] = 5
    eventResourceR4['task_application'] = 'R'
    eventResourceR4['ResourceTime'] = 1
    eventResourceR4['re_Resource'] = [4, 3, 2]
    eventResourceR4['Rrequest'] = True

    eventResourceR5 = {}
    eventResourceR5['CpuTime'] = 4
    eventResourceR5['task_application'] = 'R'
    eventResourceR5['ResourceTime'] = 1
    eventResourceR5['re_Resource'] = [3, 2, 3]
    eventResourceR5['Rrequest'] = True

    eventResourceR6 = {}
    eventResourceR6['CpuTime'] = 3
    eventResourceR6['task_application'] = 'R'
    eventResourceR6['ResourceTime'] = 1
    eventResourceR6['re_Resource'] = [2, 3, 2]
    eventResourceR6['Rrequest'] = True

    # 资源事件添加
    pcb2.event.append(eventResourceR1)
    pcb2.event.append(eventResourceR2)

    pcb3.event.append(eventResourceR3)
    pcb3.event.append(eventResourceR4)
    pcb3.event.append(eventResourceR5)
    pcb3.event.append(eventResourceR6)


    # 文件事件创建
    eventFileF1={}
    eventFileF1['CpuTime'] = 9
    eventFileF1['task_application'] = 'F'
    eventFileF1['Frequest'] = 'storage'
    eventFileF1['FileName'] = 'C\\First\\newText1.txt'
    eventFileF1['re_fSize'] = 10
    eventFileF1['Ftype'] = 0

    eventFileF2 = {}
    eventFileF2['CpuTime'] = 7
    eventFileF2['task_application'] = 'F'
    eventFileF2['Frequest'] = 'storage'
    eventFileF2['FileName'] = 'C\\First\\newText2.txt'
    eventFileF2['re_fSize'] = 10
    eventFileF2['Ftype'] = 0

    eventFileF3 = {}
    eventFileF3['CpuTime'] = 5
    eventFileF3['task_application'] = 'F'
    eventFileF3['Frequest'] = 'delete'
    eventFileF3['FileName'] = 'C\\First\\newText2.txt'
    eventFileF3['re_fSize'] = 10
    eventFileF3['Ftype'] = 0

    pcb4.event.append(eventFileF1)
    pcb4.event.append(eventFileF2)
    pcb4.event.append(eventFileF3)

    # 设置信号量
    semaphore = {'mex1': 1, 'mex2': 0}

    # 添加信号量事件
    eventSem1={}
    eventSem1['CpuTime'] = 8
    eventSem1['task_application'] = 'S'
    eventSem1['re_mutex'] = ['mex1'] #获取 1
    eventSem1['Srequest'] = True

    eventSemR1 = {}
    eventSemR1['CpuTime'] = 3
    eventSemR1['task_application'] = 'S'
    eventSemR1['re_mutex'] = ['mex2'] #释放 2 3
    eventSemR1['Srequest'] = False

    eventSem2 = {}
    eventSem2['CpuTime'] = 4
    eventSem2['task_application'] = 'S'
    eventSem2['re_mutex'] = ['mex2']  # 获取 2
    eventSem2['Srequest'] = True

    pcb4.Semaphore = semaphore
    pcb5.Semaphore = semaphore

    pcb4.event.append(eventSem1)
    pcb4.event.append(eventSemR1)
    pcb5.event.append(eventSem2)

    PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    iter = processManage('RR', PCBList)
    result = []
    while True:
        try:
            for dict in next(iter):
                result.append(dict)
            yield result
        except:
            yield result  # 暂时先这样
            break

# 进程管理

# # 测试用
# FIFSText = showFIFS()
#
# def yeildTextShow():
#     dict = next(FIFSText)
#     print(dict)


if __name__ ==  '__main__' :
    pass
    # fileShow()
    # 先进先出算法显示
    # pcb1 = PCB(1, 35, [5, 5, 5], 10)
    # pcb2 = PCB(2, 34, [5, 5, 5], 10)
    # pcb3 = PCB(3, 33, [5, 5, 5], 10)
    # pcb4 = PCB(4, 32, [5, 5, 5], 10)
    # pcb5 = PCB(5, 31, [5, 5, 5], 10)
    # PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    # complete = processManage('FIFS', PCBList)
    # print('先来先到算法测试：')
    # for i in complete:
    #     print(str(i.PID)+':'+str(i.completeTime))
    #
    # # 短作业优先算法测试(SJF)
    # pcb1 = PCB(1, 35, [5, 5, 5], 10)
    # pcb2 = PCB(2, 34, [5, 5, 5], 10)
    # pcb3 = PCB(3, 33, [5, 5, 5], 10)
    # pcb4 = PCB(4, 32, [5, 5, 5], 10)
    # pcb5 = PCB(5, 31, [5, 5, 5], 10)
    # PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    # complete = processManage('SJF', PCBList)
    # print('短作业优先算法测试：')
    # for i in complete:
    #     print(str(i.PID) + ':' + str(i.completeTime))
    #
    # # 时间片轮转算法测试
    # pcb1 = PCB(1, 35, [5, 5, 5], 10)
    # pcb2 = PCB(2, 34, [5, 5, 5], 10)
    # pcb3 = PCB(3, 33, [5, 5, 5], 10)
    # pcb4 = PCB(4, 32, [5, 5, 5], 10)
    # pcb5 = PCB(5, 31, [5, 5, 5], 10)
    # PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    # complete = processManage('RR', PCBList, 1)
    # print('时间片轮转算法测试：')
    # for i in complete:
    #     print(str(i.PID) + ':' + str(i.completeTime))
    #
    # 申请设备与同名设备测试
    # #  创建设备事件
    # eventDeviceA1 = {}
    # eventDeviceA1['CpuTime'] = 30
    # eventDeviceA1['task_application'] = 'D'
    # eventDeviceA1['re_Dname'] = '设备A1'
    # eventDeviceA1['DeviceTime'] = 5
    # eventDeviceA1['Drequest'] = True
    #
    # eventDeviceB1 = {}
    # eventDeviceB1['CpuTime'] = 30
    # eventDeviceB1['task_application'] = 'D'
    # eventDeviceB1['re_Dname'] = '设备B1'
    # eventDeviceB1['DeviceTime'] = 5
    # eventDeviceB1['Drequest'] = True
    #
    # eventDeviceB2 = {}
    # eventDeviceB2['CpuTime'] = 30
    # eventDeviceB2['task_application'] = 'D'
    # eventDeviceB2['re_Dname'] = '设备B2'
    # eventDeviceB2['DeviceTime'] = 5
    # eventDeviceB2['Drequest'] = True
    #
    # eventDeviceC1 = {}
    # eventDeviceC1['CpuTime'] = 30
    # eventDeviceC1['task_application'] = 'D'
    # eventDeviceC1['re_Dname'] = '设备C1'
    # eventDeviceC1['DeviceTime'] = 5
    # eventDeviceC1['Drequest'] = True
    #
    # eventDeviceC2 = {}
    # eventDeviceC2['CpuTime'] = 30
    # eventDeviceC2['task_application'] = 'D'
    # eventDeviceC2['re_Dname'] = '设备C2'
    # eventDeviceC2['DeviceTime'] = 5
    # eventDeviceC2['Drequest'] = True
    #
    #
    # eventDeviceC3 = {}
    # eventDeviceC3['CpuTime'] = 30
    # eventDeviceC3['task_application'] = 'D'
    # eventDeviceC3['re_Dname'] = '设备C3'
    # eventDeviceC3['DeviceTime'] = 5
    # eventDeviceC3['Drequest'] = True
    #
    # # 设备事件添加测试
    # pcb1 = PCB(1, 35, [5, 5, 5], 10)
    # pcb2 = PCB(2, 34, [5, 5, 5], 10)
    # pcb3 = PCB(3, 33, [5, 5, 5], 10)
    # pcb4 = PCB(4, 32, [5, 5, 5], 10)
    # pcb5 = PCB(5, 31, [5, 5, 5], 10)
    # #   添加同设备事件
    # pcb1.event.append(eventDeviceA1)
    # pcb2.event.append(eventDeviceA1)
    # # #   一个进程中添加多个设备申请事件
    # pcb3.event.append(eventDeviceB1)
    # pcb3.event.append(eventDeviceB2)
    # # #   在一个进程中申请多种设备
    # pcb4.event.append(eventDeviceB1)
    # pcb4.event.append(eventDeviceC1)
    # #     在一个进程中添加三种设备
    # pcb5.event.append(eventDeviceA1)
    # pcb5.event.append(eventDeviceB1)
    # pcb5.event.append(eventDeviceC1)
    #
    # PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    # complete = processManage('RR', PCBList, 1)
    # print('设备申请测试：')
    # for i in complete:
    #     print(str(i.PID) + ':' + str(i.completeTime))
    #
    #

    # # 资源事件创建
    # eventResourceR1 ={}
    # eventResourceR1['CpuTime'] = 5
    # eventResourceR1['task_application'] = 'R'
    # eventResourceR1['ResourceTime'] = 1
    # eventResourceR1['re_Resource'] = [3, 3, 3]
    # eventResourceR1['Rrequest'] = True
    #
    # eventResourceR2 ={}
    # eventResourceR2['CpuTime'] = 5
    # eventResourceR2['task_application'] = 'R'
    # eventResourceR2['ResourceTime'] = 1
    # eventResourceR2['re_Resource'] = [2, 2, 2]
    # eventResourceR2['Rrequest'] = True
    #
    # eventResourceR3 ={}
    # eventResourceR3['CpuTime'] = 5
    # eventResourceR3['task_application'] = 'R'
    # eventResourceR3['ResourceTime'] = 1
    # eventResourceR3['re_Resource'] = [1, 2, 3]
    # eventResourceR3['Rrequest'] = True
    #
    # eventResourceR4 ={}
    # eventResourceR4['CpuTime'] = 5
    # eventResourceR4['task_application'] = 'R'
    # eventResourceR4['ResourceTime'] = 1
    # eventResourceR4['re_Resource'] = [4, 3, 2]
    # eventResourceR4['Rrequest'] = True
    #
    # eventResourceR5 = {}
    # eventResourceR5['CpuTime'] = 5
    # eventResourceR5['task_application'] = 'R'
    # eventResourceR5['ResourceTime'] = 1
    # eventResourceR5['re_Resource'] = [3, 2, 3]
    # eventResourceR5['Rrequest'] = True
    #
    # eventResourceR6 = {}
    # eventResourceR6['CpuTime'] = 5
    # eventResourceR6['task_application'] = 'R'
    # eventResourceR6['ResourceTime'] = 1
    # eventResourceR6['re_Resource'] = [2, 3, 2]
    # eventResourceR6['Rrequest'] = True
    #
    # eventResourceR7 = {}
    # eventResourceR7['CpuTime'] = 5
    # eventResourceR7['task_application'] = 'R'
    # eventResourceR7['ResourceTime'] = 1
    # eventResourceR7['re_Resource'] = [1, 1, 1]
    # eventResourceR7['Rrequest'] = True
    #
    # eventResourceR8 = {}
    # eventResourceR8['CpuTime'] = 5
    # eventResourceR8['task_application'] = 'R'
    # eventResourceR8['ResourceTime'] = 1
    # eventResourceR8['re_Resource'] = [4, 4, 4]
    # eventResourceR8['Rrequest'] = True
    #
    # # 设备事件添加测试
    # pcb1 = PCB(1, 5, [5, 5, 5], 10)
    # pcb2 = PCB(2, 5, [5, 5, 5], 10)
    # pcb3 = PCB(3, 5, [5, 5, 5], 10)
    # pcb4 = PCB(4, 5, [5, 5, 5], 10)
    # pcb5 = PCB(5, 5, [5, 5, 5], 10)
    #
    # # 添加资源事件
    # pcb1.event.append(eventResourceR1)
    # pcb1.event.append(eventResourceR2)
    # pcb2.event.append(eventResourceR3)
    # pcb2.event.append(eventResourceR4)
    # pcb3.event.append(eventResourceR5)
    # pcb3.event.append(eventResourceR6)
    # pcb4.event.append(eventResourceR7)
    # pcb4.event.append(eventResourceR8)
    # PCBList = [pcb1, pcb2, pcb3, pcb4]
    # complete = processManage('FIFS', PCBList, 1)
    # print('资源申请事件：')
    # for i in complete:
    #     print(str(i.PID) + ':' + str(i.completeTime))

    # # 添加文件事件
    # eventFileF1={}
    # eventFileF1['CpuTime'] = 5
    # eventFileF1['task_application'] = 'F'
    # eventFileF1['Frequest'] = 'storage'
    # eventFileF1['FileName'] = 'C\\First\\newText.txt'
    # eventFileF1['re_fSize'] = 10
    # eventFileF1['Ftype'] = 0
    #
    # eventFileF2 = {}
    # eventFileF2['CpuTime'] = 5
    # eventFileF2['task_application'] = 'F'
    # eventFileF2['Frequest'] = 'storage'
    # eventFileF2['FileName'] = 'C\\First\\newText\\text.bin'
    # eventFileF2['re_fSize'] = 10
    # eventFileF2['Ftype'] = 0
    #
    # eventFileF3 = {}
    # eventFileF3['CpuTime'] = 3
    # eventFileF3['task_application'] = 'F'
    # eventFileF3['Frequest'] = 'delete'
    # eventFileF3['FileName'] = 'C\\First\\newText\\text.bin'
    # eventFileF3['re_fSize'] = 10
    # eventFileF3['Ftype'] = 1
    #
    # eventFileF4 = {}
    # eventFileF4['CpuTime'] = 1
    # eventFileF4['task_application'] = 'F'
    # eventFileF4['Frequest'] = 'gain'
    # eventFileF4['FileName'] = 'C\\First\\newText.txt'
    # eventFileF4['re_fSize'] = 10
    # eventFileF4['Ftype'] = 0
    #
    # # 文件事件测试
    # pcb1 = PCB(1, 5, [5, 5, 5], 10)
    # pcb2 = PCB(2, 5, [5, 5, 5], 10)
    # pcb3 = PCB(3, 5, [5, 5, 5], 10)
    # pcb4 = PCB(4, 5, [5, 5, 5], 10)
    # pcb5 = PCB(5, 5, [5, 5, 5], 10)
    #
    # # 添加文件事件
    # pcb1.event.append(eventFileF1)
    # pcb2.event.append(eventFileF2)
    # pcb3.event.append(eventFileF3)
    # pcb4.event.append(eventFileF4)
    # PCBList = [pcb1, pcb2, pcb3, pcb4]
    # complete = processManage('RR', PCBList, 1)
    # print('文件申请事件：')
    # for i in complete:
    #     print(str(i.PID) + ':' + str(i.completeTime))

    # # 设置信号量
    # semaphore = {'mex1': 1, 'mex2': 0, 'mex3': 0}
    # # 信号量同步事件
    # pcb1 = PCB(1, 5, [5, 5, 5], 10)
    # pcb2 = PCB(2, 5, [5, 5, 5], 10)
    # pcb3 = PCB(3, 5, [5, 5, 5], 10)
    #
    # pcb1.Semaphore = semaphore
    # pcb2.Semaphore = semaphore
    # pcb3.Semaphore = semaphore
    #
    # # 编写信号量事件
    # eventSem1={}
    # eventSem1['CpuTime'] = 5
    # eventSem1['task_application'] = 'S'
    # eventSem1['re_mutex'] = ['mex1'] #获取 1
    # eventSem1['Srequest'] = True
    #
    # eventSemR1 = {}
    # eventSemR1['CpuTime'] = 2
    # eventSemR1['task_application'] = 'S'
    # eventSemR1['re_mutex'] = ['mex2','mex3'] #释放 2 3
    # eventSemR1['Srequest'] = False
    #
    # eventSem2 = {}
    # eventSem2['CpuTime'] = 4
    # eventSem2['task_application'] = 'S'
    # eventSem2['re_mutex'] = ['mex2']  # 获取 2
    # eventSem2['Srequest'] = True
    #
    # eventSem3 = {}
    # eventSem3['CpuTime'] = 10
    # eventSem3['task_application'] = 'S'
    # eventSem3['re_mutex'] = ['mex3']  # 获取 3
    # eventSem3['Srequest'] = True
    #
    # pcb1.event.append(eventSem1)
    # pcb1.event.append(eventSemR1)
    # pcb2.event.append(eventSem2)
    # pcb3.event.append(eventSem3)
    #
    # PCBList = [pcb1, pcb2 ,pcb3]
    # complete = processManage('RR', PCBList, 1)
    # print('文件申请事件：')
    # for i in complete:
    #     print(str(i.PID) + ':' + str(i.completeTime))

    # 字典返回显示
    # pcb1 = PCB(1, 35, [5, 5, 5], 10)
    # pcb2 = PCB(2, 34, [5, 5, 5], 10)
    # pcb3 = PCB(3, 33, [5, 5, 5], 10)
    # pcb4 = PCB(4, 32, [5, 5, 5], 10)
    # pcb5 = PCB(5, 31, [5, 5, 5], 10)
    # PCBList = [pcb1, pcb2, pcb3, pcb4, pcb5]
    # complete = processManage('RR', PCBList)
    # print('字典返回显示')
    # while True:
    #     try:
    #         print(next(complete))
    #     except:
    #         break
    # 各种进程展示测试
    # complete = showFIFS()
    # # 方法展示测试
    # print('方法展示')
    # while True:
    #     try:
    #         print(next(complete))
    #     except:
    #         break
    #初始化
    # while True:
    #     try:
    #         yeildTextShow()
    #     except:
    #         break
